<?php
class datamodule extends module {
	
	function table() {
		$view = new view_table($this->name);
		$view->load();
		$this->output();
	}
	
	function detail() {
		$view = new view_detail($this->name);
		$view->load();
		$this->output();
	}
	
	function create() {
		$view = new view_create($this->name);
		$view->load();
		$this->output("create");
	}
	
	function edit() {
		$view = new view_edit($this->name);
		$view->load();
		$this->output("edit");
	}
	
	function delete() {
		$classname = $this->name;
		$model = new $classname();
		$model->delete(request::get("id"));
		message::setFlash("Item deleted");
		document::getInstance()->setRedirect($this->name."/table");
	}
	
	function save() {
		$classname = $this->name;
		$model = new $classname();
		$id = request::get("id");
		if (!empty($id)) {
			if ($model->update(request::get("*", "post"), $id)) {
				message::setFlash("Item updated");
				document::getInstance()->setRedirect($this->name."/detail/?id=".$id);
			} else {
				message::setMessage("Something went wrong...");
				$this->edit();
			}
		} else {
			if ($id = $model->insert(request::get("*", "post"))) {
				message::setFlash("Item created");
				document::getInstance()->setRedirect($this->name."/detail/?id=".$id);
			} else {
				message::setMessage("Something went wrong...");
				$this->create();
			}
		}
	}
	
}
?>